﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace PixelWarfare
{
    /// <summary>
    /// Creates a screen where you choose the map you want to play on.
    /// </summary>
    class MapSelectionScreen : Menu
    {
        #region Fields

        private static MapSelectionScreen theOne;  // Singleton instance variable.

        private Game game;
        private int index;

        private Rectangle imageRec;
        private bool overMap;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new MapSelectionScreen and adds all the items to the screen.
        /// </summary>
        private MapSelectionScreen()
            : base()
        {
            index = 0; // Start at the first map.

            game = Game.GetInstance();
            
            this.AddPictureItem(new Bitmap(GetType(), "img.arrow.bmp"), 
                50, 465, new OnClick(PreviousMap));

            // Flip the second arrow before adding it.
            Bitmap img = new Bitmap(GetType(), "img.arrow.bmp");
            img.RotateFlip(RotateFlipType.Rotate180FlipNone);

            this.AddPictureItem(img, 450, 465, new OnClick(NextMap));

            // The rectangle that contains the bounds of the map being drawn.
            imageRec = new Rectangle(50, 25, 400, 400);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the singleton instance of the class.
        /// </summary>
        /// <returns>Return the singleton instance.</returns>
        public static MapSelectionScreen GetInstance()
        {
            // Create it if it doesnt already exist.
            if (theOne == null)
            {
                theOne = new MapSelectionScreen();
            }

            /* Since were using it as a singleton we need to update it
             * as unexpired everytime we want to show it again. 
             * Otherwise the screen would switch immediately. 
             */
            theOne.ValidateScreen();

            return theOne;
        }

        /// <summary>
        /// Draws the map selection screen.
        /// </summary>
        /// <param name="g">Graphics to be drawn on.</param>
        public override void Draw(Graphics g)
        {
            base.Draw(g);

            // Draw a smaller version of the map
            g.DrawImage(Map.GetMap(index), imageRec);

            // If the mouse is over the map draw a semi transparent gray rectangle.
            if (overMap)
            {
                g.FillRectangle(new SolidBrush(Color.FromArgb(128, 128, 128, 128)), imageRec);
            }

            // Draw the name of the map underneath
            Utility.DrawTextCentered(g, Map.GetName(index), Color.White, 215);

        }

        /// <summary>
        /// Calls base.MouseMove() to handle the added items, then 
        /// checks if the mouse is over the map image.
        /// </summary>
        /// <param name="x">X coordinate.</param>
        /// <param name="y">Y coordinate.</param>
        public override void MouseMove(int x, int y)
        {
            base.MouseMove(x, y);

            if(imageRec.Contains(x, y))
            {
                overMap = true;
            }
            else
            {
                overMap = false;
            }
        }

        /// <summary>
        /// Calls base.MouseClick() to handle the added items, then 
        /// checks if the mouse clicked the map image and switch to
        /// the BlobSelectionScreen.
        /// </summary>
        /// <param name="x">X coordinate.</param>
        /// <param name="y">Y coordinate.</param>
        public override void MousePress(int x, int y)
        {
            base.MousePress(x, y);

            if (imageRec.Contains(x, y))
            {
                game.SetMap(Map.GetMap(index));
                InvalidateScreen(BlobSelectionScreen.GetInstance());
            }
        }

        /// <summary>
        /// Handles all key input.
        /// </summary>
        /// <param name="key">Key that was pressed.</param>
        public override void KeyPress(Keys key)
        {
            switch (key)
            {
                case Keys.Left:
                    PreviousMap();
                    break;

                case Keys.Right:
                    NextMap();
                    break;

                case Keys.Escape:
                    InvalidateScreen(MainMenuScreen.GetInstance());
                    break;

                case Keys.Enter:
                    // Activate the map before switching.
                    game.SetMap(Map.GetMap(index));
                    InvalidateScreen(BlobSelectionScreen.GetInstance());
                    break;
            }

        }

        #endregion

        #region Private Methods    

        /// <summary>
        /// Switches to the next map.
        /// </summary>
        private void NextMap()
        {
            index = (index + 1) % Map.Count;
        }

        /// <summary>
        /// Switches to the previous map.
        /// </summary>
        private void PreviousMap()
        {
            // Add maps.Count to ensure we dont take the mod of a negative number.
            index = (Map.Count + index - 1) % Map.Count;
        }

        #endregion
    }
}
